package chartRoom;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 聊天室工具类
 * 
 * @author wangsong
 *
 */
public class RoomUtil {
	/**
	 * 将指定的数据写入到当前项目的src下的msg.txt
	 * @param content
	 */
	public static void writeMessage(String content){
		File f=new File("src\\msg.txt");
		try {
	     FileOutputStream fos=new FileOutputStream(f,true);
	     OutputStreamWriter osw=new OutputStreamWriter(fos);
	     BufferedWriter bw=new BufferedWriter(osw);
		 
	     bw.write(content);
			
	     bw.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 推送消息
	 * 
	 * @param s
	 *            指定的socket
	 * @param msg
	 *            发送的消息
	 */
	public static void sendMsg(Socket s, Message msg) {
		try {
			OutputStream os = s.getOutputStream();
			ObjectOutputStream oos = new ObjectOutputStream(os);
			oos.writeObject(msg);
			oos.flush();// 清空缓存，强制发送消息
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * 获取系统当前时间
	 * 
	 * @return
	 */
	public static String queryTime() {
		Date d = new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		return df.format(d);

	}

}
